package com.mk.legoscans2;

import java.util.List;

import com.mk.legoscans2.activities.ViewScanActivity.StoreAllImagesTask;
import com.mk.legoscans2.data.AccountService;
import com.mk.legoscans2.data.ApiRepository;
import com.mk.legoscans2.data.ApplicationPreferences;
import com.mk.legoscans2.data.CachingThumbnailService;
import com.mk.legoscans2.data.DummyApiRepository;
import com.mk.legoscans2.data.ErrorReoportingSerivice;
import com.mk.legoscans2.data.FavoritesRepositoryFactory;
import com.mk.legoscans2.data.FavoritesService;
import com.mk.legoscans2.data.LocalStorageThumbnailService;
import com.mk.legoscans2.data.MessageService;
import com.mk.legoscans2.data.ThumbnailService;
import com.mk.legoscans2.data.WebApiRepository;
import com.mk.legoscans2.data.HttpThumbnailService;
import com.mk.legoscans2.data.SdCardFileService;
import com.mk.legoscans2.data.NotificationService;

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Environment;

public class LegoScans3Application extends Application {
	
	private static LegoScans3Application _instance;
	
	public LegoScans3Application(){
		_instance = this;
	}
	
	//***************************************************
	//public static String ApiDomain = "api-stage.legoscans.com";
	public static String ApiDomain = "api.legoscans.com";
	public static int ApiPort = 80;
	//public static String ApiDomain = "10.0.2.2";
	//public static int ApiPort = 50860;
	
	//***************************************************
	
	public static String GCMSenderId = "1030474604585";
	
	public static ApiRepository ApiRepository;
	public static ThumbnailService ThumbnailService;
	public static ImageManager ImageManager;
	public static FavoritesService FavoritesService;
	public static ErrorReoportingSerivice ErrorReoportingSerivice;
	public static SdCardFileService SdCardFileService;
	public static ApplicationPreferences ApplicationPreferences;
	public static MessageService MessageService;
	public static AccountService AccountService;
	public static NotificationService NotificationService;
	
	public static String SearchQuery = "";
	public static List<Scan> TempScans;
	public static Scan CurrentScan;
	public static boolean UpdateScansList = false;
	
	public static StoreAllImagesTask StoreAllImagesTask;
	
	@Override
	public void onCreate() {
		super.onCreate();
		
		ErrorReoportingSerivice = new ErrorReoportingSerivice();
		
		ApiRepository = new WebApiRepository(ErrorReoportingSerivice, ApiDomain, ApiPort);
		//ApiRepository = new DummyApiRepository();
		
		ThumbnailService = new CachingThumbnailService( new LocalStorageThumbnailService(this, new HttpThumbnailService(ErrorReoportingSerivice)) );
		ImageManager = new ImageManager(getApplicationContext(), R.drawable.logo);
		ApplicationPreferences = new ApplicationPreferences(this);
		AccountService = new AccountService(ApplicationPreferences);
		FavoritesService = new FavoritesService(new FavoritesRepositoryFactory(), AccountService);
		MessageService = new MessageService(ApiDomain, ApiPort);
		
		String sdCardPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/LegoScans/";
		SdCardFileService = new SdCardFileService(sdCardPath);
		
		NotificationService = new NotificationService(ApplicationPreferences);
	}
	
	public static Context getContext() {
    	return _instance.getApplicationContext();
    }
	
	public static int getApplicationVersionCode(){
		try {
			PackageInfo pInfo = getContext().getPackageManager().getPackageInfo(getContext().getPackageName(), 0);
			return pInfo.versionCode;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return -1;
	}
}
